<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理 -->
      <div class="role-operate">
        <el-button type="primary" size="mini" @click="showDialog = true">添加角色</el-button>
      </div>
      <!-- table标签 -->
      <el-table :data="list">
        <el-table-column prop="name" label="角色" align="center" width="200" />
        <el-table-column prop="state" label="启用" align="center" width="200">
          <!-- 自定义插槽 -->
          <template v-slot="{ row }">
            <span>
              {{ row.state === 1 ? '已启用' : row.state === 0 ? '未启用' : '未知' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center" />
        <el-table-column label="操作" align="center">
          <template>
            <!-- 放置按钮 -->
            <el-button type="text" size="mini" >分配权限</el-button>
            <el-button type="text" size="mini" >编辑</el-button>
            <el-button type="text" size="mini" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height: 60px;" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination 
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          :total="pageParams.total"
          @current-change="changePage"
          layout="prev, pager, next" >
        </el-pagination>
      </el-row>
    </div>
    <!-- 弹层 -->
    <el-dialog :visible.sync="showDialog" title="新增角色" width="500px">
      <el-form label-width="120px" ref="roleForm" :model="roleForm" :rules="rules">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" style="width: 300px;" size="mini"/>
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="roleForm.state" :active-value="1" :inactive-value="0" size="mini"/>
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="roleForm.description" type="textarea" :rows="3" tyle="width: 300px;" size="mini"/>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="isOk">确定</el-button>
              <el-button size="mini" @click="isCannel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList,addRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      showDialog: false,
      list: [],
      pageParams:{
        page: 1,
        pagesize: 5,
        total: 0
      },
      roleForm:{
        name:'',
        state: 0,
        description:''
      },
      rules:{
        name: [{ required: true,message: '角色名称不能为空', trigger: 'blur'}],
        description: [{ required: true,message: '角色描述不能为空', trigger: 'blur'}]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows,total } = await getRoleList(this.pageParams)
      this.list = rows
      this.pageParams.total = total
    },
    changePage(newPage){
      this.pageParams.page = newPage 
      this.getRoleList()
    },
    isOk(){
      
    },
    isCannel(){

    }
  }
}
</script>
<style>
.role-operate {
  padding: 10px;
}
</style>
